﻿/// <reference path="../../Scripts/jquery-1.4.1-vsdoc.js" />
/// <reference path="../../Content/Scripts/InternalMaster.js" />

var _materials = [];
//id = m.Id,
//name = m.Name,
//amount = 1,
//image = Url.Content(m.ImagePath),
//barcode = barcode,

$(function () {
	$("#materialTable").hide();

	//this method will be invoked after barcode is input.
	addBarcodeReadyHandler(function (barcode) {
		//check if its corresponding material is exsit already.
		for (var i = 0; i < _materials.length; i++) {
			var m = _materials[i];
			if (m.barcode == barcode) {
				//if it exists, add the model's amount.
				m.amount += 1;
				//i is its row index too.
				var amountTxt = $("#Amount_" + i);
				amountTxt.val(m.amount).effect("pulsate");
				return;
			}
		}

		//use ajax to get the material;
		$.post(actionUrl("MaterialInDelivery", "AJAX_GetMaterial"), { "barcode": barcode }, function (res) {
			if (res.Success) {
				var material = $.parseJSON(res.Message);
				_addMaterialRow(material);
			}
			else {
				alert(res.Message);
			}
		});
	});
});

//id=amount,id=amount,id=amount...
function _serailzeMaterials() {
	var allMaterials = "";
	$.each(_materials, function (i, m) {
		if (typeof (m) != undefined) {
			if (allMaterials.length > 0) allMaterials += ",";
			if (m.amount > 0) {
				allMaterials += m.id + "=" + m.amount;
			}
		}
	});
	return allMaterials;
}

function submitAllData() {
	var allMaterials = _serailzeMaterials()
	if (allMaterials.length == 0) {
		alert("所有物料的总量为0，不能提交。");
		return;
	}

	if (confirm("确定提交所有的物料出货量？")) {
		$("#btnConfirm").hide();
		//submit all materials;
		$.post(actionUrl("MaterialDelivery", "AJAX_SubmitMaterials"),
		{
			"allMaterials": allMaterials
		},
		function (res) {
			$("#btnConfirm").show();
			alert(res.Message);
			if (res.Success) {
				window.location = actionUrl("MaterialDelivery", "MaterialDelivery");
			}
		});
	}
}

function _addMaterialRow(material) {
	var index = _materials.length;
	_materials[index] = material;

	var htmlSegment = '\
<tr>\
	<td>\
		<img src="' + material.image + '" />\
	</td>\
	<td>\
		' + material.barcode + '\
	</td>\
	<td>\
		' + material.name + '\
	</td>\
	<td>\
		<input type="text" id="Amount_' + index + '" value="' + material.amount + '" onchange="_txtAmountChanged(this, ' + index + ')" />\
	</td>\
</tr>\
';

	$("#materialTable tbody").append(htmlSegment);
	$("#warning").hide();
	$("#materialTable").show();
	altTable($("#materialTable"));
}

function _txtAmountChanged(txt, index) {
	var m = _materials[index];
	var amount = parseInt($(txt).val());
	if (!isNaN(amount)) {
		m.amount = amount;
	}
}


//$("#poCode").ready(function () {
//	$("#btnConfirm").hide();
//	setTimeout(function () {
//		$("#OperationScreen").show();
//	}, 1000);
//	setTimeout(function () {
//		$("#materialList").show();
//		$("#btnConfirm").show();
//	}, 2000);
//	setTimeout(function () {
//		$("#textAmount").attr("value", "2");
//	}, 4000);
//	setTimeout(function () {
//		$("#textAmount").attr("value", "3");
//	}, 5000);
//	setTimeout(function () {
//		$("#materialList2").show();
//	}, 6000);
//	setTimeout(function () {
//		$("#textAmount2").attr("value", "2");
//	}, 8000);
//});

//function modifyAmount(containerId, txtId, obj) {
//	$("#" + containerId).hide();
//	$("#" + txtId).show().attr("value", $("#" + containerId).html());
//	document.getElementById(txtId).focus();
//	$("#" + txtId).focusout(goBackHandler(containerId, txtId, obj));
//}

//function goBackHandler(containerId, txtId, obj) {
//	return function () {
//		$("#" + txtId).hide();
//		$("#" + containerId).show();
//		$("#" + containerId).html($("#" + txtId).val());
//	}
//}

//function confirmAction() {
//	alert("物料出库申请提交,等待审核!");
//}
